export const useVoice = (
  params: {
    continuous?: boolean;
    onresult: (event: any) => void;
    onsoundstart?: () => void;
    lang?: string;
  } = {
    continuous: true,
    onresult: () => {},
    onsoundstart: () => {},
    lang: "zh-CN",
  }
) => {
  const isStart = ref(false);
  const SpeechRecognition =
    (window as any).webkitSpeechRecognition ||
    (window as any).SpeechRecognition;
  const speechRecognition = new SpeechRecognition();
  speechRecognition.continuous = params.continuous;
  speechRecognition.onresult = params.onresult;
  speechRecognition.lang = params.lang;
  speechRecognition.onspeechend = () => {
    isStart.value = false;
  };
  speechRecognition.onerror = () => {
    isStart.value = false;
  };
  const start = () => {
    isStart.value = true;
    speechRecognition.start();
  };
  const stop = () => {
    isStart.value = false;
    speechRecognition.stop();
  };
  return { start, stop, isStart };
};
